﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data.SqlClient;

/// <summary>
/// Summary description for PlacesManager
/// </summary>
public class PlacesManager
{
    private ArrayList listPlaces = new ArrayList();
    //private int _PlaceType;
    private int _PlaceUser;
    private int _CurrentPosition = -1;

	public PlacesManager()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public void BeforeFirst()
    {
        _CurrentPosition = -1;
    }

    public bool Next()
    {
        _CurrentPosition++;
        if (_CurrentPosition < listPlaces.Count)
            return true;
        else
            return false;
    }

    public Place GetCurrent()
    {
        return (Place)listPlaces[_CurrentPosition];
    }

    public Place GetFirstPoint()
    {
        Place p = null;
        if (listPlaces.Count > 0)
        {
            p = (Place)listPlaces[0];
        }
        return p;
    }

    public PlacesManager(int PlaceUser)
    {
        //this._PlaceType = PlaceType;
        this._PlaceUser = PlaceUser;

        try
        {
            SqlConnection conn = MapConnection.GetConnection();
            string sqlSelect = "SELECT placeid,title,description,image,longitude,latitude,type,userid FROM places WHERE userid = @userid";
            SqlCommand command = new SqlCommand(sqlSelect, conn);
            //command.Parameters.AddWithValue("@type",this._PlaceType);
            command.Parameters.AddWithValue("@userid", this._PlaceUser);

            SqlDataReader data = command.ExecuteReader();
            while (data.Read())
            {
                Place place = new Place();
                place.PlaceID = data.GetInt32(0);
                place.PlaceTitle = data.GetString(1);
                place.PlaceDescription = data.GetString(2);
                place.PlaceImage = data.GetString(3);
                place.PlaceLat = data.GetFloat(4);
                place.PlaceLng = data.GetFloat(5);
                place.PlaceType = data.GetInt32(6);
                place.PlaceUserID = data.GetInt32(7);

                listPlaces.Add(place);
            }
            
            MapConnection.CloseConnetion(conn);
        }
        catch (Exception ex)
        {
            
        }
    }
}